<?php

require ("sharedResources.php");

$sid = '';
try {
	// We need a SID (session ID) in order to operate
	$sid = login ( 'admin', 'admin' );
	echo 'sid: ' . $sid . PHP_EOL;
	

	// Requested parameters 
	// (although they are not evaluated during document creation)
	$doc ['creatorId'] = 0;
	$doc ['dateCategory'] = 0;
	$doc ['docType'] = 0;
	$doc ['exportStatus'] = 0;
	$doc ['fileSize'] = 0;
	$doc ['id'] = 0;
	$doc ['immutable'] = 0;
	$doc ['indexed'] = 0;
	$doc ['lengthCategory'] = 0;
	$doc ['publisherId'] = 0;
	$doc ['signed'] = 0;
	$doc ['size'] = 0;
	$doc ['status'] = 0; // Status = 0: document unlocked
	
	// Setting some useful Tags for the document created (tags must be separated by commas)
	$doc ['tags'] = array('document', 'creation', 'email', 'template');
	
	// This is new in 6.4	
	$doc ['published'] = 1;	
  
  
  // Setting the template ID
  // ID = 1 is the deafult template id = email (always present in LD)
	$doc ['templateId'] = 1;
  
  // Setting the template values
/*  <xs:complexType name="wsAttribute">
    <xs:sequence>
      <xs:element minOccurs="0" name="dateValue" type="xs:string"/>
      <xs:element minOccurs="0" name="doubleValue" type="xs:double"/>
      <xs:element name="editor" type="xs:int"/>
      <xs:element minOccurs="0" name="intValue" type="xs:long"/>
      <xs:element minOccurs="0" name="label" type="xs:string"/>
      <xs:element name="mandatory" type="xs:int"/>
      <xs:element minOccurs="0" name="name" type="xs:string"/>
      <xs:element name="position" type="xs:int"/>
      <xs:element minOccurs="0" name="stringValue" type="xs:string"/>
      <xs:element name="type" type="xs:int"/>
      <xs:element minOccurs="0" name="value" type="xs:anyType"/>
    </xs:sequence>
  </xs:complexType>
  
  For an explanation of the values see: http://wiki.logicaldoc.com/wiki/WSAttribute
*/    
  
  $extendedAttributes = array();  
  $wsAttribute0 = array('editor' => 0, 'mandatory' => 0, 'name' => 'subject', 'position' => 0, 'stringValue' => "T-Mobile US kills traditional cell contracts; Microsoft's traditon of confusing names | ZDNet Must-Read News - US", 'type' => 0);
  $wsAttribute1 = array('editor' => 0, 'mandatory' => 0, 'name' => 'sendername', 'position' => 0, 'stringValue' => 'ZDNet Must Read News Alerts - US', 'type' => 0);
  $wsAttribute2 = array('editor' => 0, 'mandatory' => 0, 'name' => 'from', 'position' => 0, 'stringValue' => 'newsletters@zdnet.online.com', 'type' => 0);
  $wsAttribute3 = array('editor' => 0, 'mandatory' => 0, 'name' => 'to', 'position' => 0, 'stringValue' => 'mark.reds@acme.com', 'type' => 0);
  $wsAttribute4 = array('editor' => 0, 'mandatory' => 0, 'name' => 'sentdate', 'position' => 0, 'stringValue' => '2013-03-26 18:42', 'type' => 0);
  
  $extendedAttributes[0] = $wsAttribute0;
  $extendedAttributes[1] = $wsAttribute1;
  $extendedAttributes[2] = $wsAttribute2;
  $extendedAttributes[3] = $wsAttribute3;
  $extendedAttributes[4] = $wsAttribute4;
  $doc['extendedAttributes'] = $extendedAttributes;
	
	// read the binary content of a file
	$myFile = "T-Mobile US kills traditional cell contracts; Microsoft's traditon of confusing names _ ZDNet Must-Read News - US.eml";
	$myFilePath = "C:\\tmp\\" . $myFile;
	$fh = fopen ( $myFilePath, 'r' );
	$theData = fread ( $fh, filesize ( $myFilePath ) );
	fclose ( $fh );
	//echo $theData;
	
	
    $doc ['language'] = 'it'; // This is important for indexing/search
	$doc ['fileName'] = $myFile;
	$doc ['folderId'] = DEFAULT_WORKSPACE; // create the document in DEFAULT_WORKSPACE	

	$content = $theData; // bytes?? or base64 encoded value? Don't Know BUT IT wORKS!
	
	
	$createParams = array ('sid' => $sid, 'document' => $doc, 'content' => $content );
	
	$result = $documentClient->create ( $createParams );
	print_r ( $result );
	
	echo PHP_EOL . 'Document created with Title: ' . $result->document->title . '; ID: ' . $result->document->id;
	echo PHP_EOL . 'Document fileName: ' . $result->document->fileName . '; size: ' . $result->document->fileSize;
	echo PHP_EOL . 'Document date: ' . $result->document->date;
	//unset ( $documentClient );
} catch ( SoapFault $e ) {
	print_r ( $e );
} 

// Always we need to logout in order to release connection slot
logout ( $sid );
?> 
	